﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;

public partial class CityEReport : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        //MemoryStream m = new MemoryStream();
        FileStream fs = new FileStream(Server.MapPath("~/test.pdf"), FileMode.OpenOrCreate);
        Document document = new Document();
        try
        {
            // step 2: we set the ContentType and create an instance of the Writer
            Response.ContentType = "application/pdf";
            PdfWriter write = PdfWriter.getInstance(document, fs);

            // step 3
            document.Open();

            // step 4
            iTextSharp.text.Image bmp = iTextSharp.text.Image.getInstance(Server.MapPath("~/images/logo.jpg"));
            document.Add(bmp);
            //PdfContentByte cb = write.DirectContent;
            //cb.addImage(bmp);
            //cb.stroke();

            byte[] bytes = new byte[fs.Length];
            for (int i = 0; i < bytes.Length; i++)
            {
                bytes[i] = byte.Parse(fs.ReadByte().ToString());
                fs.Position = i;
            }
            //fs.Write(bytes, 0, bytes.Length);
            
            // step 5: Close document
            //document.Close();

            Response.BinaryWrite(bytes);
            //Response.BinaryWrite(m.GetBuffer());
            //Response.OutputStream.Flush();
            //Response.OutputStream.Close();

            /**
            bmp.scalePercent(50);
            document.Add(bmp);

            Bitmap line = new Bitmap(520, 15);
            Graphics g = Graphics.FromImage(line);
            g.Clear(System.Drawing.Color.Yellow);

            System.Drawing.Font font = new System.Drawing.Font("宋体", 9);

            g.DrawString("上海市报告", font, new System.Drawing.SolidBrush(System.Drawing.Color.Black), 3, 2);
            document.Add(iTextSharp.text.Image.getInstance(line, System.Drawing.Imaging.ImageFormat.Jpeg));


            //document.Add(a);
            //document.Add("上海市简介：");
            //document.Add("上海正当我国南北弧形海岸线中部，长江与东海在此连接，交通便利，地理位置优越，是一个良好的江海港口。19世纪中叶，上海已成为商贾云集的繁华港口。如今的上海凭借其雄厚的经济实力、强大的凝聚力和辐射力，发挥着地区经济发动机的功能，带动着整个长江三角洲地区成为中国乃至东南亚的一个经济增长极。展望未来，上海将成为现代化的国际大都市和国际经济、金融、贸易、航运中心之一，在全国率先基本实现现代化。");

            BaseFont bfont = BaseFont.createFont("C:\\Windows\\Fonts\\simfang.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            iTextSharp.text.Font tfont = new iTextSharp.text.Font(bfont, 9);
            Paragraph p1 = new Paragraph(new Chunk("区域名称：上海市", tfont));
            Paragraph p2 = new Paragraph(new Phrase("This is my second paragraph.", FontFactory.getFont(FontFactory.HELVETICA, 12)));
            Paragraph p3 = new Paragraph("This is my third paragraph.", FontFactory.getFont(FontFactory.HELVETICA, 12));

            document.Add(p1);
            document.Add(p2);
            document.Add(p3);

            //document.Add(new Paragraph(Server.MapPath("./")));
            */
        }
        catch (DocumentException ex)
        {
            Console.Error.WriteLine(ex.StackTrace);
            Console.Error.WriteLine(ex.Message);
        }
        finally
        {
            document.Close();
            fs.Close();
        }
    }
}
